package LeetCode._02_数组;

import java.util.HashMap;

public class _3_无重复字符的最长子串 {
    private HashMap<Character,Integer> window = new HashMap<>();
    public int lengthOfLongestSubstring(String s) {
        if(s == null || s.length() == 0) return 0;
        int left = 0,right = 0;
        int res = 0;
        while(right<s.length()){
            char c = s.charAt(right);
            right++;
            //更新窗口数据
            window.put(c,window.getOrDefault(c,0)+1);

            //无重复，所以必须要大于1的时候才可以缩小范围
            while(window.get(c) > 1){
                char d = s.charAt(left);
                left++;
                window.put(d,window.get(d)-1);
            }
            //收缩完窗口以后，窗口没有重复元素了，保存窗口的长度
            res = Math.max(res,right - left);
        }
        return res;
    }
}
